package com.dmall.leetcode;

import java.util.HashSet;
import java.util.Set;

public class Leetcode128 {
    public int longestConsecutive(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        if (nums.length == 1) {
            return 1;
        }

        int res = 0;
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        for (Integer num : set) {
            if (set.contains(num - 1)) {
                continue;
            }
            int cur = num;
            int step = 1;
            while (set.contains(cur + 1)) {
                cur++;
                step++;
            }
            res = Math.max(res, step);
        }
        return res;
    }

    public static void main(String[] args) {
        int[] nums = {100, 4, 200, 1, 3, 2};
        Leetcode128 leetcode128 = new Leetcode128();
        System.out.println(leetcode128.longestConsecutive(nums));
    }
}
